package com.tianyi.controller.brand;


import com.tianyi.brand.BrandService;
import com.tianyi.controller.BaseController;
import com.tianyi.lib.model.WebResult;
import com.tianyi.lib.req.BrandReq;
import com.tianyi.model.SimpleLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * 品牌service
 *
 * @author cuixuhui
 * @date 2020/06/04
 */
@RestController
@RequestMapping("brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @RequestMapping(value = "addBrand", method = RequestMethod.POST)
    public WebResult<?> addBrand(@RequestBody BrandReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("user is null");
        }
        handLoginUser(req);
        return brandService.addBrand(req);
    }

    @RequestMapping(value = "deleteBrandById", method = RequestMethod.GET)
    public WebResult<?> deleteBrandById(BrandReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("user is null");
        }
        handLoginUser(req);
        return brandService.deleteBrandById(req);
    }

    @RequestMapping(value = "updateBrand", method = RequestMethod.POST)
    public WebResult<?> updateBrand(@RequestBody BrandReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("user is null");
        }
        handLoginUser(req);
        return brandService.updateBrand(req);
    }

    @RequestMapping(value = "queryBrandPage", method = RequestMethod.GET)
    public WebResult<?> queryBrandPage(BrandReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("user is null");
        }
        handLoginUser(req);
        return brandService.queryBrandPage(req);
    }

    @RequestMapping(value = "getBrandPullDown", method = RequestMethod.GET)
    public WebResult<?> getBrandPullDown(BrandReq req){

        SimpleLoginUser user = getUserFromRequest();
        if (Objects.isNull(user)){
            return WebResult.error("user is null");
        }
        handLoginUser(req);
        return brandService.getBrandPullDown(req);
    }

}
